﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace AleScheduler
{
    public partial class frmMailConfig : Form
    {
        #region Crypto Instance
        Crypto crypto = new Crypto();
        string passPhrase = "Pas5pr@seé08kk";         // can be any string
        string saltValue = "s@1tValue55bb54";         // can be any string
        string hashAlgorithm = "SHA1";                // can be "MD5"
        int passwordIterations = 2;                   // can be any number
        string initVector = "@1B2c3D4e5F6g7H8";       // must be 16 bytes
        int keySize = 256;                            // can be 192 or 128
        #endregion
        public frmMailConfig()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                checkappkey();
                RegistryKey reg = Registry.LocalMachine.OpenSubKey("Software", true).OpenSubKey("AleScheduler", true);
                RegistryKey newKey = reg.CreateSubKey("MailConfig");
                if (this.txtMailTO.Text.Trim() != string.Empty)
                    newKey.SetValue("ToMAil", this.txtMailTO.Text.Trim());
                else
                {
                    MessageBox.Show("Field 'ToMail' is Required!", "AleScheduler - eMail", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                }
                if (this.txtmailFrom.Text.Trim() != string.Empty && this.txtmailFrom.Text.Trim() != "eMail Address")
                    newKey.SetValue("FromMail", this.txtmailFrom.Text.Trim());
                else
                {
                    MessageBox.Show("Field 'FromMail' is Required!", "AleScheduler - eMail", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                }
                if (this.txtSMTP.Text.Trim() != string.Empty && this.txtSMTP.Text.Trim() != "SMTP Server")
                    newKey.SetValue("SMTPServer", this.txtSMTP.Text.Trim());
                else
                {
                    MessageBox.Show("Field 'SMTP Server' is Required!", "AleScheduler - eMail", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                }
                if (this.txtSMTPPort.Text.Trim() != string.Empty && this.txtSMTPPort.Text.Trim() != "SMTP Port")
                    newKey.SetValue("SMTPServerPort", this.txtSMTPPort.Text.Trim());
                else
                {
                    MessageBox.Show("Field 'SMTP Server Port' is Required!", "AleScheduler - eMail", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                }
                if (this.txtUsername.Text.Trim() != string.Empty)
                    newKey.SetValue("Username", this.txtUsername.Text.Trim());
                else
                    newKey.SetValue("Username", string.Empty);
                if (this.txtpassword.Text.Trim() != string.Empty)
                {
                    string crypted = crypto.Encrypt(this.txtpassword.Text.Trim(),
                                                     passPhrase,
                                                     saltValue,
                                                     hashAlgorithm,
                                                     passwordIterations,
                                                     initVector,
                                                     keySize);
                   newKey.SetValue("Password", crypted);
                }
                else
                    newKey.SetValue("Password", string.Empty);

                newKey.SetValue("SSL-TLS", this.chkSSL.Checked);

                newKey.Close();
                reg.Close();
            }
            catch
            {
                MessageBox.Show("Error writing registry !!!", "AleScheduler - eMail");
            }
            finally
            {
                this.Close();
            }
        }

        public static void checkappkey()
        {
            RegistryKey reg = Registry.LocalMachine.OpenSubKey("Software", true).OpenSubKey("AleScheduler", true);

            if (reg == null)
            {
                RegistryKey regKeyAppRoot = Registry.LocalMachine.OpenSubKey("Software", true).CreateSubKey("AleScheduler");
            }
        }

        private void frmMailConfig_Load(object sender, EventArgs e)
        {
            checkappkey();
            RegistryKey reg = Registry.LocalMachine.OpenSubKey("Software", true).OpenSubKey("AleScheduler", true).OpenSubKey("MailConfig", true);
            if (reg != null) {
                object mailTo = reg.GetValue("ToMAil");
                object mailFrom = reg.GetValue("FromMail");
                object smtpServer = reg.GetValue("SMTPServer");
                object smtpServerPort = reg.GetValue("SMTPServerPort");
                object ssltls = reg.GetValue("SSL-TLS", false);
                object username = reg.GetValue("Username");
                object password = reg.GetValue("Password");

                this.txtMailTO.Text = (mailTo is String) ? (String)mailTo : String.Empty;
                this.txtmailFrom.Text = (mailFrom is String) ? (String)mailFrom : String.Empty;
                this.txtSMTP.Text = (smtpServer is String) ? (String)smtpServer : String.Empty;
                this.txtSMTPPort.Text = (smtpServerPort is String) ? (String)smtpServerPort : String.Empty;
                this.txtUsername.Text = (username is String) ? (String)username : String.Empty;
                this.chkSSL.Checked = Convert.ToBoolean(ssltls);
                //Retrieve the cripted password and decript it
                string rawpass = (password is String) ? (String)password : String.Empty;
                if (rawpass == String.Empty)
                    this.txtpassword.Text = String.Empty;
                else {
                    string decripted = crypto.Decrypt(rawpass,
                                                        passPhrase,
                                                        saltValue,
                                                        hashAlgorithm,
                                                        passwordIterations,
                                                        initVector,
                                                        keySize);
                    this.txtpassword.Text = decripted;
                }
            }
        }
    }
}
